@@ -1,16 +0,0 @@ |
||
1 |
-class UserLocationUpdatesController < ApplicationController |
|
2 |
- skip_before_filter :authenticate_user! |
|
3 |
- |
|
4 |
- def create |
|
5 |
- user = User.find_by_id(params[:user_id]) |
|
6 |
- if user |
|
7 |
- secret = params[:secret] |
|
8 |
- user.agents.of_type(Agents::UserLocationAgent).find_all {|agent| agent.options[:secret] == secret }.each do |agent| |
|
9 |
- agent.trigger_web_request(params.except(:action, :controller, :user_id, :format), request.method_symbol.to_s, request.format.to_s) |
|
10 |
- end |
|
11 |
- render :text => "ok" |
|
12 |
- else |
|
13 |
- render :text => "user not found", :status => :not_found |
|
14 |
- end |
|
15 |
- end |
|
16 |
-end |
@@ -38,4 +38,19 @@ class WebRequestsController < ApplicationController |
||
38 | 38 |
render :text => "user not found", :status => 404 |
39 | 39 |
end |
40 | 40 |
end |
41 |
+ |
|
42 |
+ # legacy |
|
43 |
+ def update_location |
|
44 |
+ if user = User.find_by_id(params[:user_id]) |
|
45 |
+ secret = params[:secret] |
|
46 |
+ user.agents.of_type(Agents::UserLocationAgent).each { |agent| |
|
47 |
+ if agent.options[:secret] == secret |
|
48 |
+ agent.trigger_web_request(params.except(:action, :controller, :user_id, :format), request.method_symbol.to_s, request.format.to_s) |
|
49 |
+ end |
|
50 |
+ } |
|
51 |
+ render :text => "ok" |
|
52 |
+ else |
|
53 |
+ render :text => "user not found", :status => :not_found |
|
54 |
+ end |
|
55 |
+ end |
|
41 | 56 |
end |
@@ -62,10 +62,9 @@ Huginn::Application.routes.draw do |
||
62 | 62 |
|
63 | 63 |
get "/worker_status" => "worker_status#show" |
64 | 64 |
|
65 |
- post "/users/:user_id/update_location/:secret" => "user_location_updates#create" # legacy |
|
66 |
- |
|
67 |
- match "/users/:user_id/web_requests/:agent_id/:secret" => "web_requests#handle_request", :as => :web_requests, :via => [:get, :post, :put, :delete] |
|
68 |
- post "/users/:user_id/webhooks/:agent_id/:secret" => "web_requests#handle_request" # legacy |
|
65 |
+ match "/users/:user_id/web_requests/:agent_id/:secret" => "web_requests#handle_request", :as => :web_requests, :via => [:get, :post, :put, :delete] |
|
66 |
+ post "/users/:user_id/webhooks/:agent_id/:secret" => "web_requests#handle_request" # legacy |
|
67 |
+ post "/users/:user_id/update_location/:secret" => "web_requests#update_location" # legacy |
|
69 | 68 |
|
70 | 69 |
devise_for :users, :sign_out_via => [ :post, :delete ] |
71 | 70 |
get '/auth/:provider/callback', to: 'services#callback' |
@@ -1,39 +0,0 @@ |
||
1 |
-require 'spec_helper' |
|
2 |
- |
|
3 |
-describe UserLocationUpdatesController do |
|
4 |
- before do |
|
5 |
- @agent = Agent.build_for_type("Agents::UserLocationAgent", users(:bob), :name => "something", :options => { :secret => "my_secret" }) |
|
6 |
- @agent.save! |
|
7 |
- end |
|
8 |
- |
|
9 |
- it "should create events without requiring login" do |
|
10 |
- post :create, :user_id => users(:bob).to_param, :secret => "my_secret", :longitude => 123, :latitude => 45, :something => "else" |
|
11 |
- @agent.events.last.payload.should == { 'longitude' => "123", 'latitude' => "45", 'something' => "else" } |
|
12 |
- @agent.events.last.lat.should == 45 |
|
13 |
- @agent.events.last.lng.should == 123 |
|
14 |
- end |
|
15 |
- |
|
16 |
- it "should only consider Agents::UserLocationAgents for the given user" do |
|
17 |
- @jane_agent = Agent.build_for_type("Agents::UserLocationAgent", users(:jane), :name => "something", :options => { :secret => "my_secret" }) |
|
18 |
- @jane_agent.save! |
|
19 |
- |
|
20 |
- post :create, :user_id => users(:bob).to_param, :secret => "my_secret", :longitude => 123, :latitude => 45, :something => "else" |
|
21 |
- @agent.events.last.payload.should == { 'longitude' => "123", 'latitude' => "45", 'something' => "else" } |
|
22 |
- @jane_agent.events.should be_empty |
|
23 |
- end |
|
24 |
- |
|
25 |
- it "should raise a 404 error when given an invalid user id" do |
|
26 |
- post :create, :user_id => "123", :secret => "not_my_secret", :longitude => 123, :latitude => 45, :something => "else" |
|
27 |
- response.should be_missing |
|
28 |
- end |
|
29 |
- |
|
30 |
- it "should only look at agents with the given secret" do |
|
31 |
- @agent2 = Agent.build_for_type("Agents::UserLocationAgent", users(:bob), :name => "something", :options => { :secret => "my_secret2" }) |
|
32 |
- @agent2.save! |
|
33 |
- |
|
34 |
- lambda { |
|
35 |
- post :create, :user_id => users(:bob).to_param, :secret => "my_secret2", :longitude => 123, :latitude => 45, :something => "else" |
|
36 |
- @agent2.events.last.payload.should == { 'longitude' => "123", 'latitude' => "45", 'something' => "else" } |
|
37 |
- }.should_not change { @agent.events.count } |
|
38 |
- end |
|
39 |
-end |
@@ -94,4 +94,42 @@ describe WebRequestsController do |
||
94 | 94 |
post :handle_request, :user_id => users(:bob).to_param, :agent_id => 454545, :secret => "my_secret", :no => "go" |
95 | 95 |
response.should be_missing |
96 | 96 |
end |
97 |
-end |
|
97 |
+ |
|
98 |
+ describe "legacy update_location endpoint" do |
|
99 |
+ before do |
|
100 |
+ @agent = Agent.build_for_type("Agents::UserLocationAgent", users(:bob), name: "something", options: { secret: "my_secret" }) |
|
101 |
+ @agent.save! |
|
102 |
+ end |
|
103 |
+ |
|
104 |
+ it "should create events without requiring login" do |
|
105 |
+ post :update_location, user_id: users(:bob).to_param, secret: "my_secret", longitude: 123, latitude: 45, something: "else" |
|
106 |
+ @agent.events.last.payload.should == { 'longitude' => "123", 'latitude' => "45", 'something' => "else" } |
|
107 |
+ @agent.events.last.lat.should == 45 |
|
108 |
+ @agent.events.last.lng.should == 123 |
|
109 |
+ end |
|
110 |
+ |
|
111 |
+ it "should only consider Agents::UserLocationAgents for the given user" do |
|
112 |
+ @jane_agent = Agent.build_for_type("Agents::UserLocationAgent", users(:jane), name: "something", options: { secret: "my_secret" }) |
|
113 |
+ @jane_agent.save! |
|
114 |
+ |
|
115 |
+ post :update_location, user_id: users(:bob).to_param, secret: "my_secret", longitude: 123, latitude: 45, something: "else" |
|
116 |
+ @agent.events.last.payload.should == { 'longitude' => "123", 'latitude' => "45", 'something' => "else" } |
|
117 |
+ @jane_agent.events.should be_empty |
|
118 |
+ end |
|
119 |
+ |
|
120 |
+ it "should raise a 404 error when given an invalid user id" do |
|
121 |
+ post :update_location, user_id: "123", secret: "not_my_secret", longitude: 123, latitude: 45, something: "else" |
|
122 |
+ response.should be_missing |
|
123 |
+ end |
|
124 |
+ |
|
125 |
+ it "should only look at agents with the given secret" do |
|
126 |
+ @agent2 = Agent.build_for_type("Agents::UserLocationAgent", users(:bob), name: "something", options: { secret: "my_secret2" }) |
|
127 |
+ @agent2.save! |
|
128 |
+ |
|
129 |
+ lambda { |
|
130 |
+ post :update_location, user_id: users(:bob).to_param, secret: "my_secret2", longitude: 123, latitude: 45, something: "else" |
|
131 |
+ @agent2.events.last.payload.should == { 'longitude' => "123", 'latitude' => "45", 'something' => "else" } |
|
132 |
+ }.should_not change { @agent.events.count } |
|
133 |
+ end |
|
134 |
+ end |
|
135 |
+end |